package com.seemsea.web.app;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.seemsea.common.Global;
import com.seemsea.utils.DateUtils;
import com.seemsea.utils.DirUtils;
import com.seemsea.utils.qrcode.QrcodeEncoder;
import com.seemsea.web.app.baidu.QrCodeForm;

@Controller
public class AppQrCodeController {
	@RequestMapping(value="/app/qrCode.nhtml")
    public String init(Model model,HttpServletRequest request) {
		String type = request.getParameter("type"); 
		if(type == null || "".equals(type)){
			type = "seemsea";
		}
        return "/app/qrCode/"+type+"/index";
    }
    
    @RequestMapping(value="/appin/makeQrCode.nhtml", method = RequestMethod.POST)
    public void makeQrcode(QrCodeForm qrCodeForm,HttpServletResponse response,HttpServletRequest request) {
        String type = qrCodeForm.getType();
        if(type == null || "".equals(type)){
        	type = "vcard";
        }
        String code = "";
        if("vcard".equals(type)){
        	code += (char)1+""+(char)10+"CARD:";
        	String vcardName = qrCodeForm.getVcardName();
        	if(vcardName != null && !("").equals(vcardName)){
        		code += "N:"+vcardName+";";
        	}
        	String vcardJob = qrCodeForm.getVcardJob();
        	if(vcardJob != null && !("").equals(vcardJob)){
        		code += "TIL:"+vcardJob+";";
        	}
        	String vcardDep = qrCodeForm.getVcardDep();
        	if(vcardDep != null && !("").equals(vcardDep)){
        		code += "DIV:"+vcardDep+";";
        	}
        	String vcardCompany = qrCodeForm.getVcardCompany();
        	if(vcardCompany != null && !("").equals(vcardCompany)){
        		code += "COR:"+vcardCompany+";";
        	}
        	String vcardAddr = qrCodeForm.getVcardAddr();
        	if(vcardAddr != null && !("").equals(vcardAddr)){
        		code += "ADR:"+vcardAddr+";";
        	}
        	String vcardPost = qrCodeForm.getVcardPost();
        	if(vcardPost != null && !("").equals(vcardPost)){
        		code += "ZIP:"+vcardPost+";";
        	}
        	String vcardTel = qrCodeForm.getVcardTel();
        	if(vcardTel != null && !("").equals(vcardTel)){
        		code += "TEL:"+vcardTel+";";
        	}
        	String vcardMobile = qrCodeForm.getVcardMobile();
        	if(vcardMobile != null && !("").equals(vcardMobile)){
        		code += "M:"+vcardMobile+";";
        	}
        	String vcardEmail = qrCodeForm.getVcardEmail();
        	if(vcardEmail != null && !("").equals(vcardEmail)){
        		code += "EM:"+vcardEmail+";";
        	}
        	String vcardWeb = qrCodeForm.getVcardWeb();
        	if(vcardWeb != null && !("").equals(vcardWeb)){
        		code += "URL:"+vcardWeb+";";
        	}
        	//FAX:传真如053283888381p1（p后面是分机号）;
        	//IM:即时通讯工具帐号如yangjiefeng@yourcompany.com;
        }else if("sms".equals(type)){
        	code += (char)1+""+(char)10+"SMS:";
        	String smsSm = qrCodeForm.getSmsSm();
        	if(smsSm != null && !("").equals(smsSm)){
        		code += "SM:"+smsSm+";";
        	}
        	String smsTxt = qrCodeForm.getSmsTxt();
        	if(smsTxt != null && !("").equals(smsTxt)){
        		code += "TXT:"+smsTxt+";";
        	}
        }else if("email".equals(type)){
        	code += (char)1+""+(char)10+"MAIL:";
        	String mailTo = qrCodeForm.getMailTo();
        	if(mailTo != null && !("").equals(mailTo)){
        		code += "TO:"+mailTo+";";
        	}
        	String mailSub = qrCodeForm.getMailSub();
        	if(mailSub != null && !("").equals(mailSub)){
        		code += "SUB:"+mailSub+";";
        	}
        	String mailTxt = qrCodeForm.getMailTxt();
        	if(mailTxt != null && !("").equals(mailTxt)){
        		code += "TXT:"+mailTxt+";";
        	}
        }else if("bookmark".equals(type)){//网络书签
        	code += (char)1+""+(char)10+"BM:";
        	String bmSub = qrCodeForm.getBmSub();
        	if(bmSub != null && !("").equals(bmSub)){
        		code += "SUB:"+bmSub+";";
        	}
        	String bmUrl = qrCodeForm.getBmUrl();
        	if(bmUrl != null && !("").equals(bmUrl)){
        		code += "URL:"+bmUrl+";";
        	}
        }else{//文本
        	code += (char)1+""+(char)10+"DTXT:";
        	String dtxtSub = qrCodeForm.getDtxtSub();
        	if(dtxtSub != null && !("").equals(dtxtSub)){
        		code += "SUB:"+dtxtSub+";";
        	}
        	String dtxtTxt = qrCodeForm.getDtxtTxt();
        	if(dtxtTxt != null && !("").equals(dtxtTxt)){
        		code += "TXT:"+dtxtTxt+";";
        	}
        }
        Calendar cal = Calendar.getInstance();
        String folderPath = request.getSession().getServletContext().getRealPath("/")+"customFiles"+File.separator+"qrcodemake"+File.separator;
        DirUtils.makeFolder(folderPath);
        String fileName = DateUtils.formatDate(cal.getTime(), "yyyyMMddHHmmssms.")+Global.QRCODEIMGTYPE;
        try{
        	QrcodeEncoder.qrcodeEncoder(code, folderPath+fileName);
        }catch(Exception e){
        	
        }
        Map<String,Object> jsonMap = new HashMap<String,Object>();
        jsonMap.put("img", fileName);
        Gson gson = new Gson();
        String graphStr = gson.toJson(jsonMap);
        try {
			response.setContentType("text/json; charset=gbk");
			PrintWriter writer = response.getWriter();
			writer.write(graphStr);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
